package manager
{
	import com.greensock.TweenLite;
	
	import flash.utils.Dictionary;
	
	import valueObject.BarrierVO;
	import valueObject.BaseVO;
	import valueObject.BulletVO;
	import valueObject.ITarget;
	import valueObject.MonsterVO;
	import valueObject.TowerVO;

	/**
	 *子弹管理 
	 * @author SXT
	 */
	public class BulletManager extends BaseManager implements IFram
	{
		/**
		 *子弹对象池 
		 */
		public var pool:Vector.<BulletVO>;
		/**
		 *所有配置文件，
		 */		
		private var config:Dictionary;
		
		private var index:int;
		
		public function BulletManager()
		{
			super();
			MAX = 100;
			pool = new Vector.<BulletVO>;
		}
		
		public function updataConfig(level:int):void{
			config =  Game.instance.main.conl["configDic"]["bullet"];
		}
		
		public function framUpdata(index:int):void
		{
			for each(var tmp:BulletVO in pool){
				if(tmp.isActivity == false) continue;
				if(!tmp.target["isActivity"]){
					removeBullet(tmp);				//这里应该是子弹打到之前的怪物点后移出，现在是直接移出
					continue;
				}
				if(tmp.hitTestObject(tmp.target)){
					if(tmp.target is ITarget){							//是障碍物或者是怪物就攻击,前提是实现了ITarget
						tmp.target["setHp"](tmp.power);
					}
					removeBullet(tmp);
				}else{
					tmp.gotoTarget();
				}
			}
		}
		/**
		 *移出子弹 
		 * @param tmp	子弹
		 */		
		private function removeBullet(tmp:BulletVO):void
		{
			tmp.release();
			if(Game.instance.mtLayer.contains(tmp)){
				Game.instance.mtLayer.removeChild(tmp);
			}
		}
		
		public function addBullet(tower:TowerVO, bulletType:int,target:BaseVO):BulletVO{
			var bullet:BulletVO;
			if(index >= MAX){
				index = 0;
				bullet = pool[index];
				bullet.release();
			}else{
				bullet = new BulletVO();
				pool.push(bullet);
			}
			index++;
			bullet.isActivity = true;
			bullet = getConfig(bulletType,bullet);
			bullet.target = target;
			bullet.x = tower.x;
			bullet.y = tower.y;
			bullet.power = tower.power[tower.level];																	//子弹的攻击力
			Game.instance.mtLayer.addChild(bullet);
			return bullet;
		}
		
		private function getConfig(bulletType:int, bullet:BulletVO):BulletVO
		{
			for (var tmp:String in  config[bulletType]){
				try{
					bullet[tmp] =config[bulletType][tmp];
				}catch(e:Error){
					try{
						if(String(config[bulletType][tmp]).indexOf("#") != -1){
							bullet[tmp] = String(config[bulletType][tmp]).split("#");
						}else{
							bullet[tmp].push(config[bulletType][tmp]);
						}
					}catch(e:Error){
					}
				}
			}
			bullet.mc = new bullet30001;
			bullet.addChild(bullet.mc);
			return bullet;
		}
	}
}